var Icd = require('../models/icd.js');
var ccd = require('../models/ccd.js');
var user;
var icdIndex = require('../models/icdIndex.js');

exports.setUser = function(req_user){
    user = req_user;
    console.log("set User:"+user);
}
exports.test = function(res,callback){
    var project = {
        user    : "icd_Demo",
        ccd_id  : "ccd_Demo"
    }
    console.log("++++++++++++++++++");
    console.log(user);
    project.user = user;

    /*
    var icd_Demo =[
        {
            user : "icd_Demo",
            ccd_id : "ccd_Demo",

            cd: {
                "poultry-classDiagram": {_nor : 1,
                _description : "Demo of a Class-Diagram about poultries."
                }
            },
            class: {
                birdz :{
                    _nor :1,
                    _value : {bird:{_nor:1}},
                    featherz :{
                        _nor :1,
                        _value : {feather:{_nor:1}},
                        visibility: {_nor :1,
                            public: {_nor :1}
                        },
                        multiplicity:{_nor :1,
                            '*': {_nor :1}
                        }
                    },
                    teethz :
                    {
                        _nor :1,
                        _value : {teeth:{_nor:1}},
                        visibility: {_nor :1,
                            public: {_nor :1}
                        },
                        type:{_nor :1,
                            bool: {_nor :1}
                        }
                    }
                },
                geesez :{_nor :1,
                    _value : {geese:{_nor:1}},
                    flyz :{_nor :1,
                        _value : {fly:{_nor:1}},
                        visibility: {_nor :1,
                            protected: {_nor :1}
                        },
                        type:{_nor :1,
                            bool: {_nor :1}
                        }
                    }
                },
                geeseGroupz :{_nor :1,
                    _value : {geeseGroup:{_nor:1}},
                    numberz :{_nor :1,
                        _value : {number:{_nor:1}},
                        visibility: {_nor :1,
                            private: {_nor :1}
                        },
                        type:{_nor :1,
                            number: {_nor :1}
                        }
                    }
                },
                wingz :{_nor :1,
                    _value : {wing:{_nor:1}},
                    sizez :{_nor :1,
                        _value : {size:{_nor:1}},
                        visibility: {_nor :1,
                            private: {_nor :1}
                        },
                        type:{_nor :1,
                            number: {_nor :1}
                        }
                    }
                }
            },

            relation:{
                "birdz-geesez":{_nor :1,
                    _value : {"bird-geese":{_nor:1}},
                    generalization : { _nor :1,
                        multiply1:{_nor :1,
                            "1" :{_nor :1}
                        },
                        multiply2:{_nor :1,
                            "1" :{_nor :1}
                        }
                    }
                },
                "geeseGroupz-geesez":{_nor :1,
                    _value : {"geeseGroup-geese":{_nor:1}},
                    aggregation : { _nor :1,
                        multiply1:{_nor :1,
                            "1" :{_nor :1}
                        },
                        multiply2:{_nor :1,
                            "n" :{_nor :1}
                        }
                    }
                },
                "birdz-wingz":{_nor :1,
                    _value : {"bird-wing":{_nor:1}},
                    composition : { _nor :1,
                        multiply1:{_nor :1,
                            "1" :{_nor :1}
                        },
                        multiply2:{_nor :1,
                            "2" :{_nor :1}
                        }
                    }
                }
            }
        }
    ];

    var ccd_Demo =[
        {
            user : "ccd_Demo",

            cd: {
                "poultry-classDiagram": {_nor : 1,
                    _description : "Demo of a Class-Diagram about poultries."
                }
            },
            class: {
                birdz :{
                    _nor :12,
                    _value : {bird:{_nor:12}},
                    featherz :
                    {
                        _nor :12,
                        _value : {feather:{_nor:10},flume:{_nor:2}},
                        visibility: {_nor :12,
                            public: {_nor :8},
                            protected:{_nor:2},
                            private:{_nor:2}
                        },
                        multiplicity:{_nor :8,
                            '*': {_nor :4},
                            'n': {_nor :4},
                            '1-*': {_nor :0}
                        }
                    },
                    teethz :
                    {
                        _nor :10,
                        _value : {teeth:{_nor:10}},
                        visibility: {_nor :10,
                            public: {_nor :6},
                            protected:{_nor:2},
                            private:{_nor:2}
                        },
                        type:{_nor :8,
                            bool: {_nor :4},
                            boolean: {_nor :2},
                            number: {_nor:2}
                        }
                    }
                },
                geesez :{_nor :10,
                    _value : {geese:{_nor:10}},
                    flyz :{_nor :6,
                        _value : {fly:{_nor:6}},
                        visibility: {_nor :2,
                            protected: {_nor :2}
                        },
                        type:{_nor :2,
                            bool: {_nor :2}
                        }
                    }
                },
                geeseGroupz :{_nor :8,
                    _value : {geeseGroup:{_nor:8}},
                    numberz :{_nor :6,
                        _value : {number:{_nor:6}},
                        visibility: {_nor :4,
                            private: {_nor :4}
                        },
                        type:{_nor :2,
                            number: {_nor :2}
                        }
                    }
                },
                wingz :{_nor :8,
                    _value : {wing:{_nor:8}},
                    sizez :{_nor :6,
                        _value : {size:{_nor:6}},
                        visibility: {_nor :2,
                            private: {_nor :2}
                        },
                        type:{_nor :4,
                            number: {_nor :4}
                        }
                    }
                },
                feetz :{_nor :2,_value:{feet:{_nor:2}}},
                footz :{_nor:2,_value:{foot:{_nor:2}}}
            },

            relation:{
                "birdz-geesez":{_nor :1,
                    _value:{"bird-geese":{_nor:1}},
                    generalization : { _nor :1,
                        multiply1:{_nor :1,
                            "1" :{_nor :1}
                        },
                        multiply2:{_nor :1,
                            "1" :{_nor :1}
                        }
                    }
                },
                "geeseGroupz-geesez":{_nor :1,
                    _value:{"geeseGroup-geese":{_nor:1}},
                    aggregation : { _nor :1,
                        multiply1:{_nor :1,
                            "1" :{_nor :1}
                        },
                        multiply2:{_nor :1,
                            "n" :{_nor :1}
                        }
                    }
                },
                "birdz-wingz":{_nor :1,
                    _value:{"bird-wing":{_nor:1}},
                    composition : { _nor :1,
                        multiply1:{_nor :1,
                            "1" :{_nor :1}
                        },
                        multiply2:{_nor :1,
                            "2" :{_nor :1}
                        }
                    }
                }
            }
        },
        {
            user : "ccd_Demo2",

            cd: {
                "Demo_not_involved": {_nor : 1,
                    _description : "this is a demo used to illustrate the operation of project not involved"
                }
            }
        }
    ];
    */

    var icd_Demo =[
        {
            user : "icd_Demo",
            ccd_id : "ccd_Demo",

            cd: {
                "poultry-classDiagram": {_nor : 1,
                    _description : "Demo of a Class-Diagram about poultries."
                }
            },
            class: {
                bird :{
                    _nor :1,
                    _value : {bird:{_nor:1}},
                    feather :{
                        _nor :1,
                        _value : {feather:{_nor:1}},
                        visibility: {_nor :1,
                            public: {_nor :1}
                        },
                        multiplicity:{_nor :1,
                            '*': {_nor :1}
                        }
                    },
                    teeth :
                    {
                        _nor :1,
                        _value : {teeth:{_nor:1}},
                        visibility: {_nor :1,
                            public: {_nor :1}
                        },
                        type:{_nor :1,
                            bool: {_nor :1}
                        }
                    }
                },
                geese :{_nor :1,
                    _value : {geese:{_nor:1}},
                    fly :{_nor :1,
                        _value : {fly:{_nor:1}},
                        visibility: {_nor :1,
                            protected: {_nor :1}
                        },
                        type:{_nor :1,
                            bool: {_nor :1}
                        }
                    }
                },
                geeseGroup :{_nor :1,
                    _value : {geeseGroup:{_nor:1}},
                    number :{_nor :1,
                        _value : {number:{_nor:1}},
                        visibility: {_nor :1,
                            private: {_nor :1}
                        },
                        type:{_nor :1,
                            number: {_nor :1}
                        }
                    }
                },
                wing :{_nor :1,
                    _value : {wing:{_nor:1}},
                    size :{_nor :1,
                        _value : {size:{_nor:1}},
                        visibility: {_nor :1,
                            private: {_nor :1}
                        },
                        type:{_nor :1,
                            number: {_nor :1}
                        }
                    }
                }
            },

            relation:{
                "bird-geese":{_nor :1,
                    _value : {"bird-geese":{_nor:1}},
                    generalization : { _nor :1,
                        multiply1:{_nor :1,
                            "1" :{_nor :1}
                        },
                        multiply2:{_nor :1,
                            "1" :{_nor :1}
                        }
                    }
                },
                "geeseGroup-geese":{_nor :1,
                    _value : {"geeseGroup-geese":{_nor:1}},
                    aggregation : { _nor :1,
                        multiply1:{_nor :1,
                            "1" :{_nor :1}
                        },
                        multiply2:{_nor :1,
                            "n" :{_nor :1}
                        }
                    }
                },
                "bird-wing":{_nor :1,
                    _value : {"bird-wing":{_nor:1}},
                    composition : { _nor :1,
                        multiply1:{_nor :1,
                            "1" :{_nor :1}
                        },
                        multiply2:{_nor :1,
                            "2" :{_nor :1}
                        }
                    }
                }
            }
        }
    ];

    var ccd_Demo =[
        {
            user : "ccd_Demo",

            cd: {
                "poultry-classDiagram": {_nor : 1,
                    _description : "Demo of a Class-Diagram about poultries."
                }
            },
            class: {
                bird :{
                    _nor :12,
                    _value : {bird:{_nor:12}},
                    feather :
                    {
                        _nor :12,
                        _value : {feather:{_nor:10},flume:{_nor:2}},
                        visibility: {_nor :12,
                            public: {_nor :8},
                            protected:{_nor:2},
                            private:{_nor:2}
                        },
                        multiplicity:{_nor :8,
                            '*': {_nor :4},
                            'n': {_nor :4},
                            '1-*': {_nor :0}
                        }
                    },
                    teeth :
                    {
                        _nor :10,
                        _value : {teeth:{_nor:10}},
                        visibility: {_nor :10,
                            public: {_nor :6},
                            protected:{_nor:2},
                            private:{_nor:2}
                        },
                        type:{_nor :8,
                            bool: {_nor :4},
                            boolean: {_nor :2},
                            number: {_nor:2}
                        }
                    }
                },
                geese :{_nor :10,
                    _value : {geese:{_nor:10}},
                    fly :{_nor :6,
                        _value : {fly:{_nor:6}},
                        visibility: {_nor :2,
                            protected: {_nor :2}
                        },
                        type:{_nor :2,
                            bool: {_nor :2}
                        }
                    }
                },
                geeseGroup :{_nor :8,
                    _value : {geeseGroup:{_nor:8}},
                    number :{_nor :6,
                        _value : {number:{_nor:6}},
                        visibility: {_nor :4,
                            private: {_nor :4}
                        },
                        type:{_nor :2,
                            number: {_nor :2}
                        }
                    }
                },
                wing :{_nor :8,
                    _value : {wing:{_nor:8}},
                    size :{_nor :6,
                        _value : {size:{_nor:6}},
                        visibility: {_nor :2,
                            private: {_nor :2}
                        },
                        type:{_nor :4,
                            number: {_nor :4}
                        }
                    }
                },
                feet :{_nor :2,_value:{feet:{_nor:2}}},
                foot :{_nor:2,_value:{foot:{_nor:2}}}
            },

            relation:{
                "bird-geese":{_nor :1,
                    _value:{"bird-geese":{_nor:1}},
                    generalization : { _nor :1,
                        multiply1:{_nor :1,
                            "1" :{_nor :1}
                        },
                        multiply2:{_nor :1,
                            "1" :{_nor :1}
                        }
                    }
                },
                "geeseGroup-geese":{_nor :1,
                    _value:{"geeseGroup-geese":{_nor:1}},
                    aggregation : { _nor :1,
                        multiply1:{_nor :1,
                            "1" :{_nor :1}
                        },
                        multiply2:{_nor :1,
                            "n" :{_nor :1}
                        }
                    }
                },
                "bird-wing":{_nor :1,
                    _value:{"bird-wing":{_nor:1}},
                    composition : { _nor :1,
                        multiply1:{_nor :1,
                            "1" :{_nor :1}
                        },
                        multiply2:{_nor :1,
                            "2" :{_nor :1}
                        }
                    }
                }
            }
        },
        {
            user : "ccd_Demo2",

            cd: {
                "Demo_not_involved": {_nor : 1,
                    _description : "this is a demo used to illustrate the operation of project not involved"
                }
            }
        }
    ];


    var type = 'class';

    icd_Demo[0].user = user;
    ccd_Demo[0].user = user;
    ccd_Demo[1].user = user;

    var ccd_index = {};
    var icd_index = {};
    ccd.remove([null,null],function(){
        ccd.create(ccd_Demo[0],function(err,doc){
            if(doc[0] === undefined) return res.redirect("/reset");

            icd_Demo[0]["ccd_id"] = doc[0]._id;
            Icd.remove([null,null],function(){
                var filter={};
                filter["user"]="jiangsfls@163.com";
                icdIndex.remove(filter,function(err,doc){
                    icdIndex.create(filter,function(err,doc){
                        Icd.create(icd_Demo[0],function(err,doc){
                            //建立icd_index
                            ccd.create(ccd_Demo[1],function(err,doc){
                                return callback();
                            });
                        });
                    })
                });
            });
        });
    });


}
